﻿using UnityEngine;
using System.Collections;
using Dict.Blo;
using ProtoBuf.Message;

namespace UI
{

    public class UIFieldRunMapHeroInfo : MonoBehaviour
    {
        public UITexture heroIcon;
        public UILabel heroName;

        public UILabel heroHp;
        public UISprite heroHpBar;

        public UILabel heroEndurance;
        public UISprite heroEnduranceBar;

        public UISprite[] heroAngleBars;
        public PlayerSide playerSide;
        
        

        void Init()
        {
            playerSide = StaticData.fieldData.ps;
            SetHeroIcon();
            SetHeroHp();
            SetHeroAnger();
            SetHeroTired();
        }
        public void Refresh()
        {
            Init();
        }

        void SetHeroIcon()
        {
            var leaderHero = StaticData.playerData.InstPlayerData.GetBattleCamp(1).hero;

            heroIcon.GetComponent<TextureKit>().iconType = TextureIcon.HERO;
            heroIcon.GetComponent<TextureKit>().textureName = leaderHero.heroData.iconName;
            heroName.text = StaticData.playerData.name;
        }

        void SetHeroHp()
        {
            float curHp = StaticData.fieldData.currentHp;
            float maxHp = StaticData.fieldData.MaxHp;
            
            var inHp = (int)curHp;
            var inMaxHp = (int)maxHp;

            heroHp.text = inHp + "/" + "" + inMaxHp;
            heroHpBar.fillAmount = (float)curHp / (float)maxHp;

        }

        void SetHeroAnger()
        {
            float maxAngle = (float)DictConfigBlo.GetMaxAnger();
            float curAngle = (float)StaticData.fieldData.Anger;
            
            float value = curAngle * 3f / maxAngle;

            for (int i = 0; i < 3; i++)
            {
                heroAngleBars[i].fillAmount = Mathf.Clamp01(value);
                value = value - 1f;
            }
        }

        void SetHeroTired()
        {
            var var0 = StaticData.playerData.Tiredness;
            var max = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.LifeSkillTirednessReset;
            int indexPos = max.IndexOf(",");
            string cost = max.Substring(indexPos + 1);
            
            heroEndurance.text = var0 + "/" + "" + cost;

            var value = var0 / float.Parse(cost);
            heroEnduranceBar.fillAmount = value;
        }
    }
}